package com.example.beauty.service.impl;

import com.example.beauty.entity.Project;
import com.example.beauty.mapper.ProjectMapper;
import com.example.beauty.model.PageModel;
import com.example.beauty.resp.RespCode;
import com.example.beauty.resp.RespModel;
import com.example.beauty.service.ProjectService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author qin
 * @create 2022--09--07--16:20
 * PageHelper 分页插件用法：把 PageHelper.offsetPage(int offset, int limit);放在查询所有代码上方，
 * 底层会帮我们在查询所有的sql(select * from 表)后拼接 limit 字段限制条件（select * from 表 limit offset,limit）
 */
@Service
public class ProjectServiceImpl implements ProjectService {
    @Resource
    private ProjectMapper projectMapper;

    @Override
    public RespModel getAllProject(int pageNum, int limit) {
        try {
            // 计算开始索引
            int offset = (pageNum - 1) * limit;
            PageHelper.offsetPage(offset, limit);
            List<Project> projects = projectMapper.getAllProject();
            // 获取project总数
            long totalCount = projectMapper.totalCount();

            PageModel<Project> projectPage = new PageModel<>(projects, totalCount);
            return new RespModel(RespCode.SUCCESS, projectPage);
        } catch (Exception e) {
            e.printStackTrace();
            return new RespModel(RespCode.FAIL, null);
        }

    }

    @Override
    public RespModel getById(int id) {
        try {
            return new RespModel(RespCode.SUCCESS,projectMapper.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            return new RespModel(RespCode.FAIL,null);
        }
    }
}
